﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;

using System.Collections.ObjectModel;

namespace HA.ModFlow.Controls
{
    public class PCSpecifiedHeadCollecion : PCBase
    {
        public PCSpecifiedHeadCollecion(SpecifiedHeadCollecion specified)
        {
            this._SpecifiedHeadCollecion = specified;

            this._periodStep = new List<int>();

            for (int i = 0; i < specified.NPeriods; i++)
            {

                this._periodStep.Add(i + 1);

            }


            this._lstUsePrecious = new List<bool>();

            this._lstUsePrecious.AddRange(specified.IsUsePrecious);




            this._Specified = new ObservableCollection<PCSpecified>();

            foreach (SpecifiedHead item in specified.InnerList)
            {
                PCSpecified pc = new PCSpecified((SpecifiedHead)item.Clone());

                this._Specified.Add(pc);

            }
        }

        SpecifiedHeadCollecion _SpecifiedHeadCollecion;




        bool _isShowId = true;

        public bool IsShowId
        {
            get { return _isShowId; }
            set { _isShowId = value; }
        }




        List<bool> _lstUsePrecious;

        public bool UsePreciousEnabled
        {
            get
            {
                if (this._currentPeriod == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

        }

        public bool UsePrecious
        {
            get
            {
                if (_lstUsePrecious.Count == 0)
                    return false;
                return this._lstUsePrecious[this._currentPeriod];


            }
            set
            {



                this._lstUsePrecious[this._currentPeriod] = value;
                this._SpecifiedHeadCollecion.IsUsePrecious[this._currentPeriod] = value;
                this.OnPropertyChanged("UsePrecious");

            }
        }

        //具体的index值

        int _currentPeriod = 0;

        public int CurrentPeriod
        {
            get { return _currentPeriod; }
            set
            {


                _currentPeriod = value;

                this.UsePrecious = this._SpecifiedHeadCollecion.IsUsePrecious[_currentPeriod];

                foreach (PCSpecified item in this._Specified)
                {
                    item.CurrentIndex = this._currentPeriod;

                }

                this.OnPropertyChanged("UsePreciousEnabled");

                this.OnPropertyChanged("UsePrecious");


            }
        }





        List<int> _periodStep;

        public List<int> PeriodStep
        {
            get { return _periodStep; }

        }

        ObservableCollection<PCSpecified> _Specified;

        public ObservableCollection<PCSpecified> Specified
        {
            get { return _Specified; }
            set { _Specified = value; }
        }
        public void CreateNew()
        {
            SpecifiedHead Specified = new SpecifiedHead(1, 1, 1, this._SpecifiedHeadCollecion.Nrows, this._SpecifiedHeadCollecion.Ncols, this._SpecifiedHeadCollecion.Nlayers, this._periodStep.Count);

            PCSpecified pcNew = new PCSpecified(Specified);

            this.Specified.Add(pcNew);
        }


        public void ConfirmData()
        {

            List<SpecifiedHead> SpecifiedHeads = new List<SpecifiedHead>();

            foreach (PCSpecified item in this._Specified )
            {
                SpecifiedHeads.Add(item.SpecifiedHead );
             
            }

            this._SpecifiedHeadCollecion.ResetSpecifiedHeads(SpecifiedHeads);

            this._SpecifiedHeadCollecion.ResetIsUsePrecious(this._lstUsePrecious);





        }
        /// <summary>
        /// 检查层行列的正确定性
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="layer"></param>
        /// <param name="index"></param>
        /// <param name="isIndexId"></param>
        /// <returns></returns>
        public bool CheckInputIJKorIndex(int row,int col,int layer,int index,bool isIndexId)
        {
            foreach (PCSpecified item in this._Specified)
            {
                if (!isIndexId)
                {
                    if (item.I <= 0 || item.I > row)//行
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.I + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                    if (item.J <= 0 || item.J > col)
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.J + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                    if (item.K <= 0 || item.K > layer)
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.K + " 层，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                }
                else
                {
                    if (item.Index <= 0 || item.Index > index)
                    {
                        System.Windows.MessageBox.Show("不存在 " + item.Index + " 单元格ID，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }

                }
            }

            return true ;
        }

    }
    public class PCSpecified : PCBase
    {


        public PCSpecified(SpecifiedHead specified)
        {
            this._SpecifiedHead = specified;

        }

        SpecifiedHead _SpecifiedHead;

        public SpecifiedHead SpecifiedHead
        {
            get { return _SpecifiedHead; }       
        }

       


        int _currentIndex = 0;

        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {


                _currentIndex = value;


                this.OnPropertyChanged("EHead");
                this.OnPropertyChanged("SHead");
                //this.OnPropertyChanged("Elevation");



            }
        }




        public int Index
        {
            get
            {
                return this._SpecifiedHead.Index;


            }
            set
            {


                this._SpecifiedHead.Index = value;

                this.OnPropertyChanged("I");
                this.OnPropertyChanged("J");
                this.OnPropertyChanged("K");

            }
        }



        public int I
        {
            get { return this._SpecifiedHead.I; }
            set
            {
                this._SpecifiedHead.I = value;

                this.OnPropertyChanged("Index");


            }

        }


        public int J
        {
            get { return this._SpecifiedHead.J; }
            set
            {
                this._SpecifiedHead.J = value;

                this.OnPropertyChanged("Index");
            }
        }


        public int K
        {
            get { return this._SpecifiedHead.K; }
            set
            {
                this._SpecifiedHead.K = value;


                this.OnPropertyChanged("Index");

            }
        }


        public double EHead
        {
            get { return this._SpecifiedHead.EHead [this._currentIndex]; }

            set { this._SpecifiedHead.EHead[this._currentIndex] = value; }
        }


        public double SHead
        {
            get { return this._SpecifiedHead.SHead [this._currentIndex]; }

            set { this._SpecifiedHead.SHead[this._currentIndex] = value; }

        }

        //public double Elevation
        //{

        //    get { return this._SpecifiedHead.Elevation[this._currentIndex]; }

        //    set { this._SpecifiedHead.Elevation[this._currentIndex] = value; }

        //}

    }
}